package com.ugoodtech.mdcc.client.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.LevelStrUtil;
import com.ugoodtech.mdcc.client.dto.*;
import com.ugoodtech.mdcc.client.service.AliSmsProvider;
import com.ugoodtech.mdcc.client.service.ComplaintReportAuditService;
import com.ugoodtech.mdcc.client.service.ComplaintReportService;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.dto.AdminUserVo;
import com.ugoodtech.mdcc.core.dto.ComplaintSearch;
import com.ugoodtech.mdcc.core.dto.ComplaintTimeResponse;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.ComplaintSearchService;
import com.ugoodtech.mdcc.core.service.ComplaintTimeService;
import com.ugoodtech.mdcc.core.service.UserAdminService;
import com.ugoodtech.mdcc.core.service.WechatService;
import com.ugoodtech.mdcc.core.utils.ComplaintReportUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ComplaintReportServiceImpl implements ComplaintReportService {

    private Logger logger = LoggerFactory.getLogger(ComplaintReportServiceImpl.class);

    @Autowired
    private ComplaintSearchService complaintSearchService;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private StationRepository stationRepository;

    /**
     * 投诉报告Repository
     */
    @Autowired
    private ComplaintReportRepository complaintReportRepository;

    /**
     * 投诉报告答案Repository
     */
    @Autowired
    private ComplaintReportDataRepository complaintReportDataRepository;

    /**
     * 投诉报告-暂存Repository
     */
    @Autowired
    private ComplaintReportTempRepository complaintReportTempRepository;

    /**
     * 投诉报告-暂存答案Repository
     */
    @Autowired
    private ComplaintReportDataTempRepository complaintReportDataTempRepository;

    /**
     * 投诉报告-日志Repository
     */
    @Autowired
    private ComplaintReportLogRepository complaintReportLogRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private WechatService wechatService;

    @Autowired
    private ComplaintUserRepository complaintUserRepository;

    @Autowired
    private ComplaintReportAuditService complaintReportAuditService;

    @Autowired
    private ComplaintTimeService complaintTimeService;

    @Autowired
    private UserAdminService userAdminService;


    /**
     * 提交投诉单
     *
     * @param complaintReportDataList
     * @param user
     * @param tempReportId
     * @return
     */
    @Override
    public JsonResponse submitComplaintReport(ComplaintReportAnswer complaintReportDataList, User user, Long tempReportId) {
        logger.info("开始保存投诉单");
        saveComplaintReport(complaintReportDataList, user);
        if (null != tempReportId && tempReportId != 0) {
            logger.info("有暂存的投诉单,ID为：{}", tempReportId);
            ComplaintReportTemp complaintReportTemp = complaintReportTempRepository.findOne(tempReportId);
            if (null == complaintReportTemp) {
                logger.info("暂存的投诉单未找到，ID为：{}", tempReportId);
                return JsonResponse.errorResponseWithError("抱歉", "未找到该暂存的投诉单!");
            }
            complaintReportTemp.setDeleted(true);
            complaintReportTemp.setEnabled(false);
            complaintReportTempRepository.save(complaintReportTemp);
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            QComplaintReportDataTemp qComplaintReportDataTemp = QComplaintReportDataTemp.complaintReportDataTemp;
            booleanBuilder.and(qComplaintReportDataTemp.deleted.eq(false));
            booleanBuilder.and(qComplaintReportDataTemp.enabled.eq(true));
            booleanBuilder.and(qComplaintReportDataTemp.complaintReportTemp.id.eq(tempReportId));
            List<ComplaintReportDataTemp> dataTempList = (List<ComplaintReportDataTemp>) complaintReportDataTempRepository.findAll(booleanBuilder);
            for (ComplaintReportDataTemp dataTemp : dataTempList) {
                dataTemp.setDeleted(true);
                dataTemp.setEnabled(false);
                complaintReportDataTempRepository.save(dataTemp);
            }
        }
        return JsonResponse.successResponse();
    }

    /**
     * 审核不通过之后再次提交投诉报告
     *
     * @param complaintReportId
     * @param complaintReportDataList
     * @param user
     * @return
     */
    @Override
    public JsonResponse submitComplaintReport_angin(Long complaintReportId, ComplaintReportAnswer complaintReportDataList, User user) {
        ComplaintReport complaintReport = complaintReportRepository.findOne(complaintReportId);
        if (null == complaintReport) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到该投诉");
        }
        UserProfile userProfile = userProfileRepository.findByUserId(user.getId());
        if (null == userProfile || StringUtils.isEmpty(userProfile.getRealName())) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到当前登录用户");
        }
        complaintReportAuditService.updateComplaintReport(complaintReport, complaintReportDataList);
        Date nowDate = new Date();
        ComplaintReportLog complaintReportLog = new ComplaintReportLog();
        complaintReportLog.setTime(new SimpleDateFormat(Constants.DATE_TIME_FORMAT).format(nowDate));
        complaintReportLog.setUserName(userProfile.getRealName());
        complaintReportLog.setReportStatus("再次申请投诉");
        complaintReportLog.setUser(user);
        complaintReportLog.setComplaintReportId(complaintReportId);
        complaintReport.setCreationTime(new Date());
        complaintReport.setUpdateTime(new Date());
        complaintReport.setAuditFalse(false);
        complaintReportRepository.save(complaintReport);
        return JsonResponse.successResponse();
    }

    /**
     * 保存投诉单
     *
     * @param complaintReportDataList
     * @param user
     */
    private void saveComplaintReport(ComplaintReportAnswer complaintReportDataList, User user) {
        ComplaintReport complaintReport = new ComplaintReport();
        complaintReport.setUser(user);
        complaintReportRepository.save(complaintReport);
        logger.info("开始保存投诉单答案");
        Integer complaintLevel;
        String stationName = "";
        for (ComplaintReportData data : complaintReportDataList.getComplaintReportDataList()) {
            if (data.getQuestionId().equals(188L)) {
                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(data.getAnswerId()));
                if (null == answerOption || StringUtils.isEmpty(answerOption.getName())) {
                    continue;
                }
                complaintLevel = Integer.valueOf(answerOption.getName());
                complaintReport.setComplaintLevel(complaintLevel);
                complaintReportRepository.save(complaintReport);
            }
            if (data.getQuestionId().equals(187L)) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT);
                String answerCreateTime = simpleDateFormat.format(new Date());
                data.setAnswerOther(answerCreateTime);
            }
            if (data.getQuestionId().equals(192L) && StringUtils.isNotEmpty(data.getAnswerOther())) {
                Station station = stationRepository.findByStationNum(data.getAnswerOther());
                if (null != station && StringUtils.isNotEmpty(station.getName())) {
                    stationName = station.getName();
                }
            }
            data.setComplaintReport(complaintReport);
            complaintReportDataRepository.save(data);
        }
        if (StringUtils.isNotEmpty(stationName)) {
            ComplaintReportData data = complaintReportDataRepository.selectByQuestion(complaintReport.getId(), 193L);
            data.setAnswerOther(stationName);
            complaintReportDataRepository.save(data);
        }
        logger.info("开始记录日志");
        ComplaintReportLog complaintReportLog = new ComplaintReportLog();
        complaintReportLog.setComplaintReportId(complaintReport.getId());
        UserProfile userProfile = userProfileRepository.findByUserId(user.getId());
        if (null != userProfile && StringUtils.isNotEmpty(userProfile.getRealName())) {
            complaintReportLog.setUserName(userProfile.getRealName());
        }
        complaintReportLog.setUser(user);
        complaintReportLog.setReportStatus("申请投诉");
        complaintReportLog.setTime(new SimpleDateFormat(Constants.DATE_TIME_FORMAT).format(new Date()));
        complaintReportLogRepository.save(complaintReportLog);
        try {
            //获取管理员手机号
            //获取管理员手机号
            AdminUserVo adminUserVo = userAdminService.getAdminUser();
            List<User> oppenIdUser = adminUserVo.getUserList();
            String customer = getComplaintReportCustomer(complaintReport.getId());
            ComplaintReportData complaintReportData = complaintReportDataRepository.selectByQuestion(complaintReport.getId(), 194L);
            StringBuffer brandBuffer = new StringBuffer("");
            if (null != complaintReport && StringUtils.isNotEmpty(complaintReportData.getAnswerId())) {
                for (String brandIdStr : complaintReportData.getAnswerId().split(",")) {
                    if (!brandIdStr.equals("447")) {
                        QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(brandIdStr));
                        if (null != questionAnswerOption && StringUtils.isNotEmpty(questionAnswerOption.getName())) {
                            brandBuffer.append(questionAnswerOption.getName() + ",");
                        }
                    }
                }
            }
            if (StringUtils.isNotEmpty(complaintReportData.getAnswerOther())) {
                brandBuffer.append(complaintReportData.getAnswerOther());
            }
//            String level = "高级";
//            if (null != complaintReportData && StringUtils.isNotEmpty(complaintReportData.getAnswerId())) {
//                QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(complaintReportData.getAnswerId()));
//                if (null != answerOption && StringUtils.isNotEmpty(answerOption.getName())) {
//                    level = LevelStrUtil.getLevelStr(answerOption.getName());
//                }
//            }
//            aliSmsProvider.createComplaintReport(telephoneStr.toString(), userProfile.getRealName(), customer, level);
            String firstStr = "有新的投诉";
            for (User openUser : oppenIdUser) {
                wechatService.pushWechatMessage(openUser, "i0Bxtwki-LD4ytCJ-8VDO8g3ThpSHC5h3ysfSi7oh3s", Constants.WECHAT_PUSH_URL, firstStr,
                        "请尽快审批！", customer, brandBuffer.toString(), complaintReport.getCreationTimeStr(), "", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新暂存投诉单
     *
     * @param complaintReportDataList
     * @param user
     * @param tempReportId
     * @return
     */
    @Override
    public JsonResponse submitComplaintReportTemp(ComplaintReportAnswer complaintReportDataList, User user, Long tempReportId) {
        logger.info("开始暂存投诉单");
        if (null != tempReportId && tempReportId != 0) {
            logger.info("有暂存的投诉单,ID为：{}", tempReportId);
            ComplaintReportTemp complaintReportTemp = complaintReportTempRepository.findOne(tempReportId);
            if (null == complaintReportTemp) {
                logger.info("暂存的投诉单未找到，ID为：{}", tempReportId);
                return JsonResponse.errorResponseWithError("抱歉", "未找到该暂存的投诉单!");
            }
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            QComplaintReportDataTemp qComplaintReportDataTemp = QComplaintReportDataTemp.complaintReportDataTemp;
            booleanBuilder.and(qComplaintReportDataTemp.deleted.eq(false));
            booleanBuilder.and(qComplaintReportDataTemp.enabled.eq(true));
            booleanBuilder.and(qComplaintReportDataTemp.complaintReportTemp.id.eq(tempReportId));
            List<ComplaintReportDataTemp> dataTempList = (List<ComplaintReportDataTemp>) complaintReportDataTempRepository.findAll(booleanBuilder);
            logger.info("开始更新暂存的投诉单");
            for (ComplaintReportData data : complaintReportDataList.getComplaintReportDataList()) {
                for (ComplaintReportDataTemp dataTemp : dataTempList) {
                    if (data.getQuestionId().equals(dataTemp.getQuestionId())) {
                        dataTemp.setAnswerId(data.getAnswerId());
                        dataTemp.setAnswerOther(data.getAnswerOther());
                        complaintReportDataTempRepository.save(dataTemp);
                    }
                    ComplaintReportDataTemp complaintReportDataTemp = complaintReportDataTempRepository.selectDataTemp(tempReportId, data.getQuestionId());
                    if (null == complaintReportDataTemp) {
                        dataTemp = new ComplaintReportDataTemp();
                        dataTemp.setComplaintReportTemp(complaintReportTemp);
                        dataTemp.setAnswerId(data.getAnswerId());
                        dataTemp.setAnswerOther(data.getAnswerOther());
                        complaintReportDataTempRepository.save(dataTemp);
                    }
                }
            }
        } else {
            saveComplaintReportTemp(complaintReportDataList, user);
        }
        return JsonResponse.successResponse();
    }

    /**
     * 暂存投诉单
     *
     * @param complaintReportDataList
     * @param user
     */
    private void saveComplaintReportTemp(ComplaintReportAnswer complaintReportDataList, User user) {
        ComplaintReportTemp complaintReportTemp = new ComplaintReportTemp();
        complaintReportTemp.setUser(user);
        complaintReportTemp.setCreationTime(new Date());
        complaintReportTempRepository.save(complaintReportTemp);
        logger.info("开始暂存投诉单答案");
        for (ComplaintReportData data : complaintReportDataList.getComplaintReportDataList()) {
            ComplaintReportDataTemp dataTemp = new ComplaintReportDataTemp();
            BeanUtils.copyProperties(data, dataTemp);
            dataTemp.setComplaintReportTemp(complaintReportTemp);
            complaintReportDataTempRepository.save(dataTemp);
        }
    }

    /**
     * 查询投诉单
     *
     * @param pageable
     * @param complaintSearch
     * @return
     */
    @Override
    public JsonResponse getComplaintReport(Pageable pageable, ComplaintSearch complaintSearch, User clientUser) {
        List<Long> complaintIds = complaintSearchService.searchComplaintReport(complaintSearch, clientUser);
        if (complaintIds.isEmpty()) {
            return JsonResponse.successResponse();
        }
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        QComplaintReport qComplaintReport = QComplaintReport.complaintReport;
        booleanBuilder.and(qComplaintReport.deleted.eq(false));
        booleanBuilder.and(qComplaintReport.enabled.eq(true));
        booleanBuilder.and(qComplaintReport.auditFalse.eq(false));
        booleanBuilder.and(qComplaintReport.id.in(complaintIds));

        Sort sort = new Sort(Sort.Direction.DESC, "complaintReport.id", "updateTime");
        pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<ComplaintUser> complaintReportList = complaintUserRepository.findAll(booleanBuilder, pageable);
        List<ComplaintResponse> responseList = getUserResponse(complaintReportList.getContent());
        Page<ComplaintResponse> page = new PageImpl(responseList, pageable, complaintReportList.getTotalElements());
        return JsonResponse.successResponseWithPageData(page);
    }

    private List<ComplaintResponse> getUserResponse(List<ComplaintUser> complaintReportList) {
        List<ComplaintResponse> responseList = new ArrayList<>();
        for (ComplaintUser report : complaintReportList) {
            ComplaintResponse response = new ComplaintResponse();
            response.setId(report.getComplaintReport().getId());
            ComplaintReportData complaintReportData = complaintReportDataRepository.selectByQuestion(report.getId(), 187L);
            if (null != complaintReportData && StringUtils.isNotEmpty(complaintReportData.getAnswerOther())) {
                response.setTime(complaintReportData.getAnswerOther());
            } else {
                response.setTime(report.getComplaintReport().getUpdateTimeStr());
            }
            response.setType(ComplaintReportUtil.getReportStatus(report.getComplaintReport().getReportStatus()));
            if (null != report.getUser()) {
                UserProfile userProfile = userProfileRepository.findByUserId(report.getUser().getId());
                response.setUserName(userProfile.getRealName());
            } else {
                response.setUserName("未领取");
            }
            ComplaintTimeResponse timeResponse = complaintTimeService.getComplaintTime(report.getComplaintReport());
            response.setActualTime(timeResponse.getTotalTime());
            response.setTimeRemaining(timeResponse.getRemainingTime());
            response.setFollowUserTime(timeResponse.getActualProcessingTime());
            response.setCustomerName(getComplaintReportCustomer(report.getComplaintReport().getId()));
            response.setValidTime(report.getComplaintReport().getValidTime().toString());
            responseList.add(response);
        }
        return responseList;
    }

    @Override
    public String getComplaintReportCustomer(Long complaintReportId) {
        ComplaintReportData reportData = complaintReportDataRepository.selectByQuestion(complaintReportId, 193L);
        String customerName = "客户";
        if (null != reportData && StringUtils.isNotEmpty(reportData.getAnswerOther())) {
            customerName = reportData.getAnswerOther();
        }
        reportData = complaintReportDataRepository.selectByQuestion(complaintReportId, 192L);
        if (null != reportData && StringUtils.isNotEmpty(reportData.getAnswerOther())) {
            Station station = stationRepository.findByStationNum(reportData.getAnswerOther());
            if (null != station && StringUtils.isNotEmpty(station.getName())) {
                return station.getName();
            }
        }
        return customerName;
    }

    /**
     * 封裝查詢投诉单
     *
     * @param complaintReportList
     * @return
     */
    private List<ComplaintResponse> getResponse(List<ComplaintReport> complaintReportList) {
        List<ComplaintResponse> responseList = new ArrayList<>();
        for (ComplaintReport report : complaintReportList) {
            ComplaintResponse response = new ComplaintResponse();
            if (null != report.getValidTime()) {
                response.setValidTime(report.getValidTime().toString());
            }
            response.setId(report.getId());
            response.setTime(report.getUpdateTimeStr());
            response.setType(ComplaintReportUtil.getReportStatus(report.getReportStatus()));
            if (null != report.getFollowUser()) {
                UserProfile userProfile = userProfileRepository.findByUserId(report.getFollowUser().getId());
                response.setUserName(userProfile.getRealName());
            }
            if (report.isAuditFalse()) {
                response.setAuditStatus("审核不通过");
                if (StringUtils.isNotEmpty(report.getAuditFalseNote())) {
                    response.setAuditNote(report.getAuditFalseNote());
                }
            } else {
                if (report.isAudit()) {
                    response.setAuditStatus("审核通过");
                } else {
                    response.setAuditStatus("审核中");
                }
            }
            if (null != report.getFirstAuditTime()) {
                ComplaintTimeResponse timeResponse = complaintTimeService.getComplaintTime(report);
                response.setActualTime(timeResponse.getTotalTime());
                response.setTimeRemaining(timeResponse.getRemainingTime());
                response.setFollowUserTime(timeResponse.getActualProcessingTime());
            }
            ComplaintReportData reportData = complaintReportDataRepository.selectByQuestion(report.getId(), 193L);
            if (null != reportData && StringUtils.isNotEmpty(reportData.getAnswerOther())) {
                response.setCustomerName(reportData.getAnswerOther());
            }
            responseList.add(response);
        }
        return responseList;
    }

    /**
     * 获取客户已经提交的投诉
     *
     * @param user
     * @param pageable
     * @return
     */
    @Override
    public JsonResponse getMyComplaintReport(User user, Pageable pageable) {
        BooleanBuilder builder = new BooleanBuilder();
        QComplaintReport qComplaintReport = QComplaintReport.complaintReport;
        builder.and(qComplaintReport.deleted.eq(false));
        builder.and(qComplaintReport.enabled.eq(true));
        builder.and(qComplaintReport.user.id.eq(user.getId()));
        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<ComplaintReport> complaintReportList = complaintReportRepository.findAll(builder, pageable);
        List<ComplaintResponse> responseList = getResponse(complaintReportList.getContent());
        Page<ComplaintResponse> page = new PageImpl(responseList, pageable, complaintReportList.getTotalElements());
        return JsonResponse.successResponseWithPageData(page);
    }

    /**
     * 获取我的暂存投诉单
     *
     * @param user
     * @param pageable
     * @return
     */
    @Override
    public JsonResponse getMyTempComplaintReport(User user, Pageable pageable) {
        BooleanBuilder builder = new BooleanBuilder();
        QComplaintReportTemp qComplaintReportTemp = QComplaintReportTemp.complaintReportTemp;
        builder.and(qComplaintReportTemp.deleted.eq(false));
        builder.and(qComplaintReportTemp.enabled.eq(true));
        builder.and(qComplaintReportTemp.user.id.eq(user.getId()));
        Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
        pageable = new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), sort);
        Page<ComplaintReportTemp> tempList = complaintReportTempRepository.findAll(builder, pageable);
        List<ComplaintTempResponse> responseList = getTempReport(tempList.getContent());
        Page<ComplaintTempResponse> page = new PageImpl(responseList, pageable, tempList.getTotalElements());
        return JsonResponse.successResponseWithPageData(page);
    }

    /**
     * 封装暂存投诉单
     *
     * @param tempList
     * @return
     */
    private List<ComplaintTempResponse> getTempReport(List<ComplaintReportTemp> tempList) {
        List<ComplaintTempResponse> responseList = new ArrayList<>();
        for (ComplaintReportTemp reportTemp : tempList) {
            ComplaintTempResponse response = new ComplaintTempResponse();
            response.setId(reportTemp.getId());
            response.setCreateTime(reportTemp.getCreationTimeStr());
            ComplaintReportDataTemp dataTemp = complaintReportDataTempRepository.selectDataTemp(reportTemp.getId(), 195L);
            if (null != dataTemp && StringUtils.isNotEmpty(dataTemp.getAnswerId())) {
                StringBuffer stringBuffer = new StringBuffer("");
                for (String answerId : dataTemp.getAnswerId().split(",")) {
                    QuestionAnswerOption answerOption = questionAnswerOptionRepository.findOne(Long.valueOf(answerId));
                    if (null != answerOption && StringUtils.isNotEmpty(answerOption.getName())) {
                        stringBuffer.append(answerOption.getName() + "");
                    }
                }
                response.setComplaintType(stringBuffer.toString());
            }
            ComplaintReportDataTemp reportData = complaintReportDataTempRepository.selectDataTemp(reportTemp.getId(), 193L);
            if (null != reportData && StringUtils.isNotEmpty(reportData.getAnswerOther())) {
                response.setCustomerName(reportData.getAnswerOther());
            }
            UserProfile userProfile = userProfileRepository.findByUserId(reportTemp.getUser().getId());
            if (null != userProfile && StringUtils.isNotEmpty(userProfile.getRealName())) {
                response.setUserName(userProfile.getRealName());
            }
            responseList.add(response);
        }
        return responseList;
    }

    /**
     * 删除暂存的投诉单
     *
     * @param complaintTempReportId
     * @return
     */
    @Override
    public JsonResponse deleteComplaintReport(User user, Long complaintTempReportId) {
        ComplaintReportTemp reportTemp = complaintReportTempRepository.findOne(complaintTempReportId);
        if (!reportTemp.getUser().getId().equals(user.getId())) {
            return JsonResponse.errorResponseWithError("抱歉", "无法删除不是自己的报告");
        }
        if (null == reportTemp) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到该报告！");
        }
        reportTemp.setDeleted(true);
        reportTemp.setEnabled(false);
        complaintReportTempRepository.save(reportTemp);
        List<ComplaintReportDataTemp> tempList = complaintReportDataTempRepository.selectAnswer(complaintTempReportId);
        for (ComplaintReportDataTemp dataTemp : tempList) {
            dataTemp.setDeleted(true);
            dataTemp.setEnabled(false);
            complaintReportDataTempRepository.save(dataTemp);
        }
        return JsonResponse.successResponse();
    }

    @Override
    public JsonResponse getComplaintFollowUser(Long complaintReportId) {
        ComplaintReport complaintReport = complaintReportRepository.findOne(complaintReportId);
        if (null == complaintReport) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到该投诉单");
        }
        if (null != complaintReport.getLeader()) {
            ComplaintFollowUser followUser = new ComplaintFollowUser();
            followUser.setId(complaintReport.getLeader());
            followUser.setVailTime(complaintReport.getValidTime());
            UserProfile userProfile = userProfileRepository.findByUserId(complaintReport.getLeader());
            if (null != userProfile && StringUtils.isNotEmpty(userProfile.getRealName())) {
                followUser.setName(userProfile.getRealName());
            }
            return JsonResponse.successResponseWithData(followUser);
        } else if (!complaintReport.getNeedUserList().isEmpty()) {
            List<ComplaintFollowUser> complaintFollowUserList = new ArrayList<>();
            for (Long userId : complaintReport.getNeedUserList()) {
                ComplaintFollowUser followUser = new ComplaintFollowUser();
                followUser.setId(userId);
                followUser.setVailTime(complaintReport.getValidTime());
                UserProfile userProfile = userProfileRepository.findByUserId(userId);
                if (null != userProfile && StringUtils.isNotEmpty(userProfile.getRealName())) {
                    followUser.setName(userProfile.getRealName());
                    complaintFollowUserList.add(followUser);
                }
            }
            return JsonResponse.successResponseWithData(complaintFollowUserList);
        }
        return JsonResponse.errorResponseWithError("抱歉", "未找到该投诉单");
    }

    @Override
    public JsonResponse giveUpReport(Long complaintReportId) {
        ComplaintReport complaintReport = complaintReportRepository.findOne(complaintReportId);
        if (null == complaintReport) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到该投诉单");
        }
        complaintReport.setDeleted(true);
        complaintReport.setEnabled(false);
        complaintReportRepository.save(complaintReport);
        return JsonResponse.successResponse();
    }


}
